/**
 * <p>文件名:		MailSenderImpl.java</p>
 * <p>版权:		CopyrightTag</p>
 * <p>公司:		千方集团CTFO</p>
 * @author		周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 */

package com.caits.lbs.framework.services.mail;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import org.apache.log4j.Logger;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;

import com.caits.lbs.framework.log.CommonLogFactory;
import com.caits.lbs.framework.utils.StringUtils;

/**
 * <p>
 * MailSenderImpl
 * </p>
 * <p>
 * 用途：通用邮件发送服务实现类
 * 
 * </p>
 * 
 * @author 周华彬(zhouhuabin@ctfo.com, zhou_hua_bin@163.com)
 * @version 0.0.1 2011-8-11
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">版本号</th>
 *          <th width="100px">动作</th>
 *          <th width="100px">修改人</th>
 *          <th width="100px">修改时间</th>
 *          </tr>
 *          <!-- 以 Table 方式书写修改历史 -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-8-11 下午3:56:13</td>
 *          </tr>
 *          <tr>
 *          <td>0.0.1</td>
 *          <td>创建类</td>
 *          <td>zhb</td>
 *          <td>2011-8-11 下午3:56:13</td>
 *          <td>0.0.2</td>
 *          <td>修改类</td>
 *          <td>xxx</td>
 *          <td>x年x月x日</td>
 *          </tr>
 *          </table>
 */

public class MailSenderImpl implements IMailService {

	/** 日志记录器 */
	private Logger log = CommonLogFactory.getLog();
	/** 发送人地址 */
	private String fromAddress;

	/** 邮件发送器接口 */
	private JavaMailSender jmailSender;

	/**
	 * 发送普通的文本信息 jmail相当于服务器,发送的内容由自己来封装
	 */
	public void sendTextMail(String toAddress, String subject, String text) {
		try {
			SimpleMailMessage simmess = new SimpleMailMessage();
			simmess.setFrom(getFromAddress());
			simmess.setTo(StringUtils.splitToArray(toAddress, ",", true));
			simmess.setSubject(subject);
			simmess.setText(text);
			jmailSender.send(simmess);
		} catch (MailException e) {
			log.error("发送文本邮件错误," + e.getLocalizedMessage() + "," + toAddress,
					e);
			return;
		}
		log.info("发送文本邮件到" + toAddress);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.caits.lbs.framework.services.mail.IMailService#sendHTMLMail(java.
	 * lang.String, java.lang.String, java.lang.String)
	 */
	public void sendHTMLMail(String toAddress, String subject, String text) {
		MimeMessage message = jmailSender.createMimeMessage();
		MimeMessageHelper helper;
		try {
			helper = new MimeMessageHelper(message, true, "GB2312");
			helper.setFrom(getFromAddress());
			helper.setTo(StringUtils.splitToArray(toAddress, ",", true));
			helper.setSubject(subject);
			helper.setText(text, true);
			jmailSender.send(message);
		} catch (MessagingException e) {
			log.error("发送网页邮件错误," + e.getLocalizedMessage() + "," + toAddress,
					e);
			return;
		}
		log.info("成功发送网页邮件到" + toAddress);
	}

	/**
	 * 带附件的发送
	 * 
	 * @param toAddress
	 * @param subject
	 * @param text
	 * @param files
	 */
	public void sendAttachmentMail(final String toAddress,
			final String subject, final String text, final List<String> files) {

		// 匿名内部类所引用的外部变量必须为只读final
		MimeMessagePreparator mimeMail = new MimeMessagePreparator() {

			public void prepare(MimeMessage mimeMess) throws Exception {
				// 设置收件人和标题
				String[] addressArray = StringUtils.splitToArray(toAddress,
						",", true);
				Address[] toAddressArray = new InternetAddress[addressArray.length];
				for (int i = 0; i < toAddressArray.length; i++) {
					toAddressArray[i] = new InternetAddress(addressArray[i]);
				}
				mimeMess.setRecipients(Message.RecipientType.TO, toAddressArray);
				mimeMess.setSubject(subject);
				// 设置发送人信息
				mimeMess.setFrom(new InternetAddress(getFromAddress()));
				Multipart mp = new MimeMultipart();
				// 向邮件中添加正文.
				MimeBodyPart context = new MimeBodyPart();
				context.setText(text);
				// 向MimeMessage添加（Multipart代表正文）
				mp.addBodyPart(context);

				// 附件列表
				for (String filename : files) {
					MimeBodyPart attfile = new MimeBodyPart();
					FileDataSource fds = new FileDataSource(filename);
					attfile.setDataHandler(new DataHandler(fds));
					attfile.setFileName(fds.getName());
					mp.addBodyPart(attfile);
				}
				// MimeMessage 添加Multipart
				mimeMess.setContent(mp);
				mimeMess.setSentDate(new Date());
			}
		};
	try{
		// 发送带附件的邮件
		jmailSender.send(mimeMail);
	} catch (MailException e) {
		log.error("发送附件邮件错误," + e.getLocalizedMessage() + "," + toAddress,
				e);
		return;
	}
		log.info("发送附件邮件到" + toAddress);
	}

	/**
	 * 获取变量<code>fromAddress</code>的值
	 * 
	 * @return 返回的数据类型是<code>String</code>
	 */
	public String getFromAddress() {
		return fromAddress;
	}

	/**
	 * 设置变量<code> fromAddress</code> 的值
	 * 
	 * @param fromAddress
	 *            <code>fromAddress</code> 参数类型是<code>String</code>
	 */
	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	/**
	 * 获取变量<code>jmailSender</code>的值
	 * 
	 * @return 返回的数据类型是<code>JavaMailSender</code>
	 */
	public JavaMailSender getJmailSender() {
		return jmailSender;
	}

	/**
	 * 设置变量<code> jmailSender</code> 的值
	 * 
	 * @param jmailSender
	 *            <code>jmailSender</code> 参数类型是<code>JavaMailSender</code>
	 */
	public void setJmailSender(JavaMailSender jmailSender) {
		this.jmailSender = jmailSender;
	}

	/**
	 * @param simpleMessage
	 * @throws MailException
	 * @see org.springframework.mail.MailSender#send(org.springframework.mail.SimpleMailMessage)
	 */
	public void send(SimpleMailMessage simpleMessage) throws MailException {
		jmailSender.send(simpleMessage);
	}

	/**
	 * @param simpleMessages
	 * @throws MailException
	 * @see org.springframework.mail.MailSender#send(org.springframework.mail.SimpleMailMessage[])
	 */
	public void send(SimpleMailMessage[] simpleMessages) throws MailException {
		jmailSender.send(simpleMessages);
	}

	/**
	 * @return
	 * @see org.springframework.mail.javamail.JavaMailSender#createMimeMessage()
	 */
	public MimeMessage createMimeMessage() {
		return jmailSender.createMimeMessage();
	}

	/**
	 * @param contentStream
	 * @return
	 * @throws MailException
	 * @see org.springframework.mail.javamail.JavaMailSender#createMimeMessage(java.io.InputStream)
	 */
	public MimeMessage createMimeMessage(InputStream contentStream)
			throws MailException {
		return jmailSender.createMimeMessage(contentStream);
	}

	/**
	 * @param mimeMessage
	 * @throws MailException
	 * @see org.springframework.mail.javamail.JavaMailSender#send(javax.mail.internet.MimeMessage)
	 */
	public void send(MimeMessage mimeMessage) throws MailException {
		jmailSender.send(mimeMessage);
	}

	/**
	 * @param mimeMessages
	 * @throws MailException
	 * @see org.springframework.mail.javamail.JavaMailSender#send(javax.mail.internet.MimeMessage[])
	 */
	public void send(MimeMessage[] mimeMessages) throws MailException {
		jmailSender.send(mimeMessages);
	}

	/**
	 * @param mimeMessagePreparator
	 * @throws MailException
	 * @see org.springframework.mail.javamail.JavaMailSender#send(org.springframework.mail.javamail.MimeMessagePreparator)
	 */
	public void send(MimeMessagePreparator mimeMessagePreparator)
			throws MailException {
		jmailSender.send(mimeMessagePreparator);
	}

	/**
	 * @param mimeMessagePreparators
	 * @throws MailException
	 * @see org.springframework.mail.javamail.JavaMailSender#send(org.springframework.mail.javamail.MimeMessagePreparator[])
	 */
	public void send(MimeMessagePreparator[] mimeMessagePreparators)
			throws MailException {
		jmailSender.send(mimeMessagePreparators);
	}

	/**
	 * FIXME
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
